package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Device;
import com.ruoyi.system.domain.DeviceDTO;
import com.ruoyi.system.domain.DeviceQueryParams;
import com.ruoyi.system.domain.DeviceView;
import com.ruoyi.system.service.IDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 设备管理Controller
 * 
 * @author ruoyi
 * @date 2025-01-15
 */
@RestController
@RequestMapping("/system/device")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 导出设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:device:export')")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Device device)
    {
        List<Device> list = deviceService.selectDeviceList(device);
        ExcelUtil<Device> util = new ExcelUtil<Device>(Device.class);
        util.exportExcel(response, list, "设备管理数据");
    }

    /**
     * 获取设备管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:device:query')")
    @GetMapping(value = "/{deviceId}")
    public AjaxResult getInfo(@PathVariable("deviceId") Long deviceId)
    {
        return success(deviceService.selectDeviceByDeviceId(deviceId));
    }

    /**
     * 新增设备管理
     */
    @PreAuthorize("@ss.hasPermi('system:device:add')")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Device device)
    {
        return toAjax(deviceService.insertDevice(device));
    }

    /**
     * 修改设备管理
     */
        @PutMapping("/update")
        public ResponseEntity<?> updateDevice(@RequestBody DeviceDTO dto) {
            try {
                int result = deviceService.updateDevice(dto);
                if (result > 0) {
                    return ResponseEntity.ok().body("更新成功");
                } else {
                    return ResponseEntity.status(HttpStatus.NOT_FOUND).body("设备不存在");
                }
            } catch (RuntimeException e) {
                return ResponseEntity.badRequest().body(e.getMessage());
            }
        }

    /**
     * 删除设备管理
     */
    @PreAuthorize("@ss.hasPermi('system:device:remove')")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deviceIds}")
    public AjaxResult remove(@PathVariable Long[] deviceIds)
    {
        return toAjax(deviceService.deleteDeviceByDeviceIds(deviceIds));
    }

    /**
     * 根据区域ID查询设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:device:list')")
    @GetMapping("/listByAreaId/{areaId}")
    public TableDataInfo listByAreaId(@PathVariable("areaId") Long areaId)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceListByAreaId(areaId);
        return getDataTable(list);
    }

    /**
     * 根据矿工ID查询设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:device:list')")
    @GetMapping("/listByMineId/{mineId}")
    public TableDataInfo listByMineId(@PathVariable("mineId") Long mineId)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceListByMineId(mineId);
        return getDataTable(list);
    }

//        /**
//         * 根据省份查询设备管理列表
//         */
//        @GetMapping("/listByProvince/{province}")
//        public TableDataInfo listByProvince(@PathVariable("province") String province) {
//            startPage();
//            List<DeviceView> list = deviceService.selectDeviceListByProvince(province);
//            return getDataTable(list);
//        }

    /**
     * 根据设备类型ID查询设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:device:list')")
    @GetMapping("/listByDeviceTypeId/{deviceTypeId}")
    public TableDataInfo listByDeviceTypeId(@PathVariable("deviceTypeId") Long deviceTypeId)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceListByDeviceTypeId(deviceTypeId);
        return getDataTable(list);
    }

    /**
     * 查询设备总数
     */
    @PreAuthorize("@ss.hasPermi('system:device:list')")
    @GetMapping("/count")
    public AjaxResult count()
    {
        int count = deviceService.selectDeviceCount();
        return AjaxResult.success(count);
    }

    /**
     * 根据设备状态统计数量
     *
     * @param status 设备状态（正常/故障/离线）
     * @return 包含统计数量的响应结果
     */
    @PreAuthorize("@ss.hasPermi('system:device:list')")
    @Log(title = "设备管理", businessType = BusinessType.OTHER)
    @GetMapping("/countByStatus/{status}")
    public AjaxResult countByStatus(@PathVariable String status) {
        int count = deviceService.selectDeviceCountByStatus(status);
        return AjaxResult.success().put("count", count);
    }

    /**
     * 获取所有状态设备数量统计
     */
    @PreAuthorize("@ss.hasPermi('system:device:list')")
    @GetMapping("/statusCounts")
    public AjaxResult getDeviceStatusCounts() {
        return AjaxResult.success(deviceService.getDeviceStatusCounts());
    }
    // DeviceController.java

//    @GetMapping("/view")
//    public TableDataInfo getDeviceView() {
//        startPage();
//        return getDataTable(deviceService.getDeviceView()) ;
//        }
@GetMapping("/view")
public TableDataInfo getDeviceView(DeviceQueryParams params) {
    startPage(); // 分页逻辑
    List<DeviceView> list = deviceService.getDeviceView(params);
    return getDataTable(list);
}

}
